package com.neusoft.yuejian_6.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.neusoft.yuejian_6.MainActivity;
import com.neusoft.yuejian_6.R;
import com.neusoft.yuejian_6.javabean.User;
import com.neusoft.yuejian_6.model.UserModel;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;







    public class LoginActivity extends AppCompatActivity {


        private EditText username;
        private EditText password;

        private Button loginButton,regbutton;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
            initViews();
            initEvents();
        }
        public void initViews(){
            username = (EditText) findViewById(R.id.username);
            password = (EditText) findViewById(R.id.password);

            loginButton = (Button) findViewById(R.id.loginbutton);
        }
        public void initEvents() {
            Button regbutton = (Button)findViewById(R.id.regbutton);
            regbutton.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View view) {
                    Intent intent =new Intent();
                    intent.setClass(LoginActivity.this,RegActivity.class);
                    startActivityForResult(intent,1);

                }
            });



            loginButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    UserModel userModel = new UserModel();

                    //1.发送请求
                    Call<User> call=userModel.login
                            (username.getText().toString(),password.getText().toString());
                    //2.处理结果
                    Callback<User> callback=new Callback<User>() {
                        @Override
                        public void onResponse(Call<User> call, Response<User> response) {
                            User user = response.body();
                            if (!"0".equals(user.getUser_id())) {
                                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                                saveUser(user.getUser_id());
                                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                startActivity(intent);
                                finish();

                            } else {
                                Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<User> call, Throwable t) {

                        }
                    };
                    //3.call and callback 捆绑
                    call.enqueue(callback);
                }
            });
        }

        public void saveUser(int user_id){
            SharedPreferences sharedPreferences=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor=sharedPreferences.edit();
            editor.putString("user_name",username.getText().toString());
            //  u_id = putInt(u_id);
            editor.putInt("user_id",user_id);
            // System.out.print(u_id);
            editor.putString("user_password",password.getText().toString());
            editor.commit();

        }


    }






